package com.gary.service;

import com.alibaba.fastjson.JSON;
import com.gary.entity.AppHealthCheckResult;
import io.prometheus.client.Gauge;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author liliang
 * @date 2022/03/11
 **/
@Slf4j
@Service
public class AppHealthCheckExporterService {

    @Autowired
    private Gauge appHealthCheckExporter;

    public void exporterPrometheusMetrics() {
        appHealthCheckExporter.clear();

        for (int i = 0; i < 3; i++) {
            AppHealthCheckResult result = new AppHealthCheckResult();
            result.setTarget("172.20.2.14"+i+":9089");
            result.setIdc("广州机房");
            result.setCluster("智慧医院");
            result.setApplication("fuse-center");
            result.setStatus("UP");
            result.setValue(0);
            result.setCheckTime(new Date().toString());
            Gauge.Child child = appHealthCheckExporter.labels(result.getTarget(),
                    result.getIdc(),
                    result.getCluster(),
                    result.getApplication(),
                    result.getCheckTime(),
                    result.getStatus());
            child.set(result.getValue());
        }

        appHealthCheckExporter.collect();
    }

}
